﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace RaProWorld
{
    class KomaZiegen_objectMap
    {
        protected int width;
        protected int height;
        protected int sectorsize;
        protected int[][][] map;

        public KomaZiegen_objectMap(float width, float height, int sectorsize)
        {
            this.width = (int)(width / sectorsize);
            this.height = (int)(height / sectorsize);
            this.sectorsize = sectorsize;

            map = new int[this.width][][];
            for (int x = 0; x < this.width; x++)
            {
                map[x] = new int[this.height][];
                for (int y = 0; y < this.height; y++)
                {
                    map[x][y] = new int[2];
                    map[x][y][0] = -1;
                    map[x][y][1] = -1;
                }
            }
        }

        public void reset()
        {
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    map[i][j][0] = -1;
                    map[i][j][1] = -1;
                }
            }
        }
        
        public void update(List<KomaZiegen_goat> goats)
        {
            int gx, gy, gs;
            for (int i = 0; i < goats.Count; i++)
            {
                gx = (int)(goats[i].Position.X / sectorsize);
                gy = (int)(goats[i].Position.Y / sectorsize);
                gs = (int)(goats[i].Size / sectorsize);

                for (int n = gx; n <= gx + gs; n++)
                    for (int m = gy; m <= gy + gs; m++)
                        if (n < width && m < height && n >= 0 && m >= 0)
                            map[n][m][0] = i;
            }
        }

        public void update(List<Rectangle> toasts)
        {
            int tx, ty, tw, th;
            for (int i = 0; i < toasts.Count; i++)
            {
                tx = (toasts[i].X / this.sectorsize);
                ty = (toasts[i].Y / this.sectorsize);
                tw = (toasts[i].Width / this.sectorsize);
                th = (toasts[i].Height / this.sectorsize);

                for (int n = tx; n <= tx + tw; n++)
                {
                    for (int m = ty; m <= ty + th; m++)
                    {
                        if (n < this.width && m < this.height && n >= 0 && m >= 0)
                        {
                            map[n][m][1] = i;
                        }
                    }
                }
            }
        }

        public int goatIndexAt(float x, float y)
        {
            int xSec = (int)(x / this.sectorsize);
            int ySec = (int)(y / this.sectorsize);

            try
            {
                return map[xSec][ySec][0];
            }
            catch (IndexOutOfRangeException)
            {
                if (xSec >= this.width) return map[this.width - 1][ySec][0];
                else if (ySec >= this.height) return map[xSec][this.height - 1][0];
                else return map[this.width - 1][this.height - 1][0];
            }
        }

        public int toastIndexAt(float x, float y)
        {
            int xSec = (int)(x / this.sectorsize);
            int ySec = (int)(y / this.sectorsize);

            try
            {
                return map[xSec][ySec][1];
            }
            catch (IndexOutOfRangeException)
            {
                if (xSec >= this.width) return map[this.width - 1][ySec][1];
                else if (ySec >= this.height) return map[xSec][this.height - 1][1];
                else return map[this.width - 1][this.height - 1][1];
            }
        }
    }
}